#!/bin/bash

DEPLOY_DIR=/home/stevejrong/deploy # 【部署脚本根目录，需视情况更改】
SHELL_ROOT_DIR=$DEPLOY_DIR/deploy_shell # 【无需更改】独立出来是为了部署完后可删除，保持服务器整洁
COMPONENT_NAME=redis_cluster # 【此次要部署组件名称，也就是其目录名称，需视情况更改】
u_COMPONENT_DEPLOY_SHELL_ROOT_DIR=$SHELL_ROOT_DIR/$COMPONENT_NAME # 【部署脚本目录，无需更改】e.g: /home/stevejrong/deploy /deploy_shell/ redis_cluster

PODMAN_INSTANCE_DATA_DIR=$DEPLOY_DIR/podman_instance_data #【一般无需更改】为了和部署脚本的根目录区分开，保持服务器整洁 e.g: /home/stevejrong/deploy /podman_instance_data
u_COMPONENT_PODMAN_INSTANCE_DATA_DIR=$PODMAN_INSTANCE_DATA_DIR/$COMPONENT_NAME # 【组件数据目录，无需更改】e.g: /home/stevejrong/deploy /podman_instance_data /redis_cluster

u_COMPONENT_TEMPLATE_FILE_DIR=$u_COMPONENT_DEPLOY_SHELL_ROOT_DIR/config_files # 【部署脚本中的模板文件目录，无需更改】e.g: /home/stevejrong/deploy /deploy_shell/ redis_cluster /config_files

u_DEPLOY_FOR_PODMAN_INSTANCE_NAMES_ARRAY=("redis-cluster-s11-n1" "redis-cluster-s11-n2") # 【需视情况更改】要部署实例的容器名数组
u_DEPLOY_FOR_PODMAN_INSTANCE_PORTS_SEED_ARRAY=(1101 11011) # 【需视情况更改】要部署实例的容器端口种类种子数组。生成N个实例，端口种子值就会递增N次

u_DEPLOY_FOR_PODMAN_INSTANCE_INTERNAL_NETWORK_PREFIX='10.2.2.'
u_DEPLOY_FOR_PODMAN_INSTANCE_INTERNAL_NETWORK_GATEWAY=$u_DEPLOY_FOR_PODMAN_INSTANCE_INTERNAL_NETWORK_PREFIX'0/24'
u_DEPLOY_FOR_PODMAN_INSTANCE_INTERNAL_NETWORK_SEED=11 #【需视情况更改】要部署实例的容器，其内网IP地址后缀的种子。生成实例时，由此值开始，依次递增IP地址的后缀，为容器生成内网IP地址
u_DEPLOY_FOR_PODMAN_INSTANCE_INTERNAL_NETWORK_NAME='redis_cluster_network'

u_SERVER_IP=192.168.56.11

# clean
echo -e ' >>>>>>>>>>>>>>>>>>>>>> Clean Podman instance...'
for ((i = 0; i < ${#u_DEPLOY_FOR_PODMAN_INSTANCE_NAMES_ARRAY[@]}; i++)); do
  podman stop ${u_DEPLOY_FOR_PODMAN_INSTANCE_NAMES_ARRAY[$i]} \
    && podman rm ${u_DEPLOY_FOR_PODMAN_INSTANCE_NAMES_ARRAY[$i]}
done
echo -e ' >>>>>>>>>>>>>>>>>>>>>> Done!'

echo -e ' >>>>>>>>>>>>>>>>>>>>>> Clean Podman internal network...'
podman network rm $u_DEPLOY_FOR_PODMAN_INSTANCE_INTERNAL_NETWORK_NAME
podman network create --subnet $u_DEPLOY_FOR_PODMAN_INSTANCE_INTERNAL_NETWORK_GATEWAY $u_DEPLOY_FOR_PODMAN_INSTANCE_INTERNAL_NETWORK_NAME
echo -e ' >>>>>>>>>>>>>>>>>>>>>> Done!'

echo -e ' >>>>>>>>>>>>>>>>>>>>>> Clean old dir and file...'
groovy delete_file.groovy ---$u_COMPONENT_PODMAN_INSTANCE_DATA_DIR \
                          ---$u_COMPONENT_DEPLOY_SHELL_ROOT_DIR/podman-compose.yml
echo -e ' >>>>>>>>>>>>>>>>>>>>>> Done!'

# generate redis.conf
echo -e ' >>>>>>>>>>>>>>>>>>>>>> Generate redis.conf...'
for ((i = 0; i < ${#u_DEPLOY_FOR_PODMAN_INSTANCE_NAMES_ARRAY[@]}; i++)); do
  # 节点名：${u_DEPLOY_FOR_PODMAN_INSTANCE_NAMES_ARRAY[$i]}
  # 端口种子1：${u_DEPLOY_FOR_PODMAN_INSTANCE_PORTS_SEED_ARRAY[0]}
  # 端口种子2：${u_DEPLOY_FOR_PODMAN_INSTANCE_PORTS_SEED_ARRAY[1]}
  groovy generate_file.groovy ---port1=${u_DEPLOY_FOR_PODMAN_INSTANCE_PORTS_SEED_ARRAY[0]} \
                              ---port2=${u_DEPLOY_FOR_PODMAN_INSTANCE_PORTS_SEED_ARRAY[1]} \
                              ---config-path=node.conf \
                              --source-path=$u_COMPONENT_TEMPLATE_FILE_DIR/redis.conf.template \
                              --target-path=$u_COMPONENT_PODMAN_INSTANCE_DATA_DIR/${u_DEPLOY_FOR_PODMAN_INSTANCE_NAMES_ARRAY[$i]}/conf/redis.conf

  # 端口种子自增
  u_DEPLOY_FOR_PODMAN_INSTANCE_PORTS_SEED_ARRAY[0]=$((${u_DEPLOY_FOR_PODMAN_INSTANCE_PORTS_SEED_ARRAY[0]} + 1))
  u_DEPLOY_FOR_PODMAN_INSTANCE_PORTS_SEED_ARRAY[1]=$((${u_DEPLOY_FOR_PODMAN_INSTANCE_PORTS_SEED_ARRAY[1]} + 1))
done
echo -e ' >>>>>>>>>>>>>>>>>>>>>> Done!'

# generate and append podman-compose.yml
echo -e ' >>>>>>>>>>>>>>>>>>>>>> Generate podman-compose.yml...'
for ((i = 0; i < ${#u_DEPLOY_FOR_PODMAN_INSTANCE_NAMES_ARRAY[@]}; i++)); do
  groovy generate_file.groovy ---service-name=${u_DEPLOY_FOR_PODMAN_INSTANCE_NAMES_ARRAY[$i]} ---root-dir=$u_COMPONENT_PODMAN_INSTANCE_DATA_DIR ---podman-bind-ipv4-address=$u_DEPLOY_FOR_PODMAN_INSTANCE_INTERNAL_NETWORK_PREFIX$u_DEPLOY_FOR_PODMAN_INSTANCE_INTERNAL_NETWORK_SEED ---podman-network-name=$u_DEPLOY_FOR_PODMAN_INSTANCE_INTERNAL_NETWORK_NAME ---podman-ipam-config-subnet=$u_DEPLOY_FOR_PODMAN_INSTANCE_INTERNAL_NETWORK_GATEWAY --source-path=$u_COMPONENT_TEMPLATE_FILE_DIR/podman --target-path=$u_COMPONENT_DEPLOY_SHELL_ROOT_DIR/podman-compose.yml

  u_DEPLOY_FOR_PODMAN_INSTANCE_INTERNAL_NETWORK_SEED=$((${u_DEPLOY_FOR_PODMAN_INSTANCE_INTERNAL_NETWORK_SEED} + 1))
done
echo -e ' >>>>>>>>>>>>>>>>>>>>>> Done!'

# deploy redis instance
echo -e ' >>>>>>>>>>>>>>>>>>>>>> Executing podman-compose.yml...'
podman-compose -f $u_COMPONENT_DEPLOY_SHELL_ROOT_DIR/podman-compose.yml up -d
echo -e ' >>>>>>>>>>>>>>>>>>>>>> Done!'

# output podman instance
podman ps -a
echo -e ' >>>>>>>>>>>>>>>>>>>>>> ☺ All done!'
